// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;


import RankManager from "./RankManager";
import Logger from "../Logger/Logger";
import Utils from "../common/Util";
import GameManager from "../common/GameManager";
import { lang_zh, LANG_ENUM } from "../i18n/zh";
import { GameConfig } from "../common/GameConfig";
import Facebook from "../Facebook/Facebook";

@ccclass
export default class GameRank extends cc.Component {

    private friendRankList = null;

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Node)
    arrow: cc.Node = null;

    @property(cc.Label)
    prompt: cc.Label = null;

    @property(cc.Node)
    avatar: cc.Node = null;

    @property(cc.Label)
    nameLable: cc.Label = null;

    @property(cc.Label)
    score: cc.Label = null;

    @property(cc.Node)
    share: cc.Node = null;


    onLoad() {
        Logger.log("GameRank RegisterEvent")
        GameManager.getInstance().eventCenter.RegisterEvent(this.uuid, "updateScore", this.updateCurrentScore.bind(this));
        this.share.on(cc.Node.EventType.TOUCH_START, this.shareWithFriend, this)
    }

    onDisable() {
        this.share.off(cc.Node.EventType.TOUCH_START, this.shareWithFriend, this)
    }

    private ensureFriendRankList() {
        if(this.friendRankList && this.friendRankList.length > 0) {
            return;
        }
        let friendList = RankManager.getInstance().getFriendRankList();
        if (friendList.length == 0) {
            Logger.log("getFriendRankList is empty!")
            return;
        }
        if (typeof FBInstant != 'undefined') {
            friendList.forEach(function (item, index, arr) {
                if (item.playerId == FBInstant.player.getID()) {
                    arr.splice(index, 1);
                }
            });
        }
        this.friendRankList = friendList;
        Logger.log("updateCurrentScore : " + friendList);
    }

    public updateCurrentScore(score) {
        Logger.log("updateCurrentScore : " + score);
        if(!score) {
            return;
        }
        this.ensureFriendRankList();
        if(this.friendRankList == null || this.friendRankList.length <= 0) {
            Logger.log("there is no friend ranks!")
            this.node.active = false;
            return;
        }
        this.node.active = true;
        let nextRank;
        for (let i = 0; i < this.friendRankList.length; i++) {
            nextRank = this.friendRankList[i];
            if (nextRank.playerScore < score) {
                if (i == 0) {//已经是第一名
                    nextRank = null;
                } else {
                    nextRank = this.friendRankList[i - 1];
                }
                break;
            }
        }

        if (nextRank) {
            this.updateOtherRanks(nextRank.playerPicUrl, nextRank.playerName, nextRank.playerScore);
        } else {
            this.updateTopRanks(score)
        }

    }

    private updateTopRanks(score) {
        Logger.log("updateTopRanks : " + score);
        this.prompt.string = lang_zh["champion"][GameConfig.languages];
        if (typeof FBInstant != 'undefined') {
            Utils.LoadHeadImg(this.avatar, FBInstant.player.getPhoto());
            this.nameLable.string = Utils.cutNick(FBInstant.player.getName(), 8);
        }
        this.score.string = score;
    }

    private updateOtherRanks(url, name, score) {
        Logger.log("updateOtherRanks : " + url + ";" + score + ";" + name);
        this.prompt.string = lang_zh["surpass"][GameConfig.languages];
        Utils.LoadHeadImg(this.avatar, url);
        this.nameLable.string = Utils.cutNick(name, 8);
        this.score.string = score;
    }

    private shareWithFriend() {
        Facebook.ChooseAsync();
    }

}
